// 2462. 雇佣 K 位工人的总代价
class Solution {
public:
    long long totalCost(vector<int>& costs, int k, int candidates) {
        int n = costs.size();
        if(2*candidates > n) 
        {
            sort(costs.begin(), costs.end());
            return accumulate(costs.begin(), costs.begin()+k, 0LL);
        }

        using pii = pair<int, int>;
        priority_queue<pii, vector<pii>, greater<pii>> pq;
        for(int i = 0; i < candidates; ++i)
        {
            pq.emplace(costs[i], i);
            pq.emplace(costs[n-i-1], n-i-1);
        }
        long long ret = 0;
        int left = candidates, right = n - candidates - 1;
        while(k--)
        {
            auto [cost, i] = pq.top();
            pq.pop();
            ret += cost;
            if(left > right) continue;
            if(i < right) pq.emplace(costs[left], left++);
            else pq.emplace(costs[right], right--);
        }
        return ret;
    }
};

// 1318. 或运算的最小翻转次数
class Solution {
public:
    int minFlips(int a, int b, int c) {
        int ret = 0;
        for(int i = 0; i < 31; ++i)
        {
            int bitA = (a >> i) & 1;
            int bitB = (b >> i) & 1;
            int bitC = (c >> i) & 1;
            if(0 == bitC) ret += (bitA + bitB);
            else ret += (0 == bitA + bitB);
        }
        return ret;
    }
};